package org.example.api;

import lombok.extern.slf4j.Slf4j;
import org.example.data.form.ProductForm;
import org.example.entity.ProductEntity;
import org.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/get")
    public Object getProduct(
            @Validated ProductForm productForm){

        return productService.getProduct(productForm.getId());

    }

    @RequestMapping("/add")
    public Object addProduct(
            ProductForm productParam){

        ProductEntity productEntity = new ProductEntity();

        productEntity.setName(productParam.getName());
        productEntity.setProduceTimestamp(productParam.getProduceTimestamp());

        ProductEntity result = productService.addProduct(productEntity);

        return result;

    }

    @RequestMapping("/update")
    public Object updateProduct(
             ProductForm productForm){

        ProductEntity productEntity = new ProductEntity();

        productEntity.setId(productForm.getId());
        productEntity.setName(productForm.getName());

        ProductEntity result = productService.updateProduct(productEntity);

        return result;

    }


}
